<template>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh" :disabled="!needRefresh">
    <van-list
      v-model="loading"
      :finished="finished"
      @load="onLoad"
    >
      <div v-for="(d,idx) in data" :key="idx">
        <slot name="item" :data="d" />
      </div>
    </van-list>
    <div v-if="data.length==0" class="empty">
      <span>没有数据</span>
    </div>
  </van-pull-refresh>
</template>

<script>
const defaultOption = {
  needLoadMore: true,
  needRefresh: true
}
export default {
  name: 'BaseList',
  props: {
    option: {
      type: Object,
      default: () => {
        return defaultOption
      }
    }
  },
  computed: {
    needLoadMore() {
      return this.option.hasOwnProperty('needLoadMore')
        ? this.option.needLoadMore
        : defaultOption.needLoadMore
    },
    needRefresh() {
      return this.option.hasOwnProperty('needRefresh')
        ? this.option.needRefresh
        : defaultOption.needRefresh
    }
  },
  data() {
    return {
      refreshing: false,
      finished: false,
      loading: false,
      data: []
    }
  },
  methods: {
    onLoad() {
      this.$emit('load', '1')
    },
    onRefresh() {
      this.$emit('load', '0')
    },
    onDataChange(data, noMore) {
      ;(this.data = data), (this.finished = noMore)
      this.refreshing = false
      this.loading = false
    }
  }
}
</script>

<style scoped>
.empty {
  position: absolute;
  left: 0;
  right: 0;
  top: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
